﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using Ext.Net;
using System.Web.Caching;
using Model;
using System.Collections.Generic;
using BLL;
using System.IO;
/*
*程序员悲剧莫过于此，新东西出来了，就要马上学习
*程序员最大的悲剧莫过于系统做到一半了，新东西版本升级了
*程序员最最大的悲哀莫过于更新后的版本尽然不兼容以前的版本
*2010-11-2日，coolite升级为Ext.net，不得已重写了一遍系统，特此一记
*/
namespace Web.View
{
    public partial class backMgr : System.Web.UI.Page
    {
        protected void Page_PreLoad(object sender, EventArgs e)
        {
            if (Session["currentUserID"] == null || Session["userpwd"] == null || Session["role"] == null)
                Response.Redirect("/View/Login.aspx");
            if (!IsPostBack)
                ThemeChange();
        }
        //登录
        protected void BtnSave_Click(object sender, DirectEventArgs e)
        {
            if (TxtNewPwd.Text.Trim().Length <= 0 || TxtOldPwd.Text.Trim().Length <= 0)
            {
                MsgShow("提示", "信息不完整！");
                return;
            }
            if (TxtSurePwd.Text != TxtNewPwd.Text)
            { MsgShow("提示", "两次新密码输入不一致！"); return; }
            userinfoMgr userinfoMgr = new userinfoMgr();
            userinfo user = userinfoMgr.FindById_userinfo(Session["username"].ToString());
            //Md5+盐值加密算法，相对安全
            if (user != null && user.password.Trim() == CJ_DBOperater.CJ.PwdSecurity(Server.UrlEncode(this.TxtOldPwd.Text.Trim().Replace("'", ""))))
            {
                user.password = CJ_DBOperater.CJ.PwdSecurity(Server.UrlEncode(this.TxtSurePwd.Text.Trim().Replace("'", "")));
                if (userinfoMgr.Update_userinfo(user))
                {
                    MsgShow("成功", "密码修改成功，请重新登录系统！");
                    Response.Redirect("/View/Login.aspx");
                    return;
                }
                MsgShow("错误", "密码修改失败，请重试！");
            }
            else
                MsgShow("错误", "旧密码错误，请重新输入！");
        }
        //退出系统
        protected void BtnExit_Click(object sender, DirectEventArgs e)
        {
            Session.Clear();
            Response.Redirect("/View/Login.aspx");
        }
        /// <summary>
        /// 获取用户角色功能菜单树
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GetExamplesNodes(object sender, NodeLoadEventArgs e)
        {
            if (e.NodeID == "root")
            {
                rolefunMgr rolefunMgr = new rolefunMgr();
                if (Session["role"] != null && Session["role"].ToString().Trim().Length > 0)
                    e.Nodes = rolefunMgr.GetMenu(Session["role"].ToString());
            }
        }


        //保存皮肤到cookie
        protected void cbTheme_changed(object sender, DirectEventArgs e)
        {
            Response.Cookies["theme"].Value = cbTheme.SelectedItem.Value.ToString();
            Response.Cookies["theme"].Expires = DateTime.Now.AddDays(100d);
        }
        /// <summary>
        /// 皮肤切换后保存到cookie中，下次登录加载皮肤
        /// </summary>
        private void ThemeChange()
        {
            if (Request.Cookies["theme"] != null)
            {
                switch (Request.Cookies["theme"].Value)
                {
                    case "gray":
                        HttpContext.Current.Session["Ext.net.Theme"] = Ext.Net.Theme.Gray;
                        cbTheme.SetValue("gray");
                        break;
                    case "access":
                        HttpContext.Current.Session["Ext.net.Theme"] = Ext.Net.Theme.Access;
                        cbTheme.SetValue("access");
                        break;
                    case "slate":
                        HttpContext.Current.Session["Ext.net.Theme"] = Ext.Net.Theme.Slate;
                        cbTheme.SetValue("slate");
                        break;
                    default:
                        HttpContext.Current.Session["Ext.net.Theme"] = Ext.Net.Theme.Default;
                        cbTheme.SetValue("default");
                        break;
                }
            }
            if (Session["username"] != null)
            {
                this.Panel1.Title = "尊敬的<font color='red'>" + Session["username"].ToString() + "</font>  欢迎您使用“中国云--WebMisDeveloper”";
            }
        }
        /// <summary>
        /// 弹出窗口方法
        /// </summary>
        /// <param name="title">窗口标题</param>
        /// <param name="infos">提示内容</param>
        public void MsgShow(string title, string infos)
        {
            X.Msg.Show(new MessageBoxConfig
            {
                Title = title,
                Message = infos,
                Buttons = MessageBox.Button.OK,
                Icon = MessageBox.Icon.INFO,
                AnimEl = this.BtnSave.ClientID
            });
            
        }

    }
}
